package com.wawei.seeds.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wawei.seeds.R;
import com.wawei.seeds.model.UserInfo;

import java.util.List;

/**
 * Created by itachi on 15/7/17.
 */
public class CommentItemAdapter extends BaseAdapter {
    private List<UserInfo> info;
    private LayoutInflater mLayoutInflater;

    public CommentItemAdapter(List<UserInfo> info, Context context) {
        mLayoutInflater = LayoutInflater.from(context);
        this.info = info;
    }

    @Override
    public int getCount() {
        return info == null ? 0 : info.size();
    }

    @Override
    public UserInfo getItem(int position) {
        return info.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommentItemViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new CommentItemViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.layout_comment_item,
                    parent, false);
            viewHolder.nameView = (TextView)convertView.findViewById(R.id.tv_comment_name);
            viewHolder.commentView = (TextView)convertView.findViewById(R.id.tv_comment_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (CommentItemViewHolder) convertView.getTag();
        }

        UserInfo item = getItem(position);
        if (viewHolder != null && viewHolder.nameView != null) {
            viewHolder.nameView.setText(item.name + ":");
        }
        if (viewHolder != null && viewHolder.commentView != null) {
            viewHolder.commentView.setText(item.comment);
        }

        return convertView;
    }

    private static class CommentItemViewHolder {
        TextView nameView;
        TextView commentView;
    }
}
